package view.frontend.order;

import exceptionManager.ExceptionManager;
import java.awt.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.text.*;
import model.CustomerSpecified;
import model.OrderLine;
import view.generaldesign.DesignLayout;

/*
 @author Mathias and Sonny Sandberg
 */
public class OrderOverview {

    private JPanel 
            overviewPanel,
            productPanel,
            scrollProductPanel;
    private OrderContainer orderContainer;

    public OrderOverview(OrderContainer view) 
    {
        orderContainer = view;

        overviewPanel = new JPanel(null);
        overviewPanel.setBounds(320, 0, 580, 500);
        overviewPanel.setOpaque(false);
        overviewPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Ordreoversigt", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));
        
        buildOverviewPanel();
        
        overviewPanel.setVisible(true);
    }
    
    public void buildOverviewPanel()
    {
        overviewPanel.removeAll();
        
        if (orderContainer.getOrder() != null)
        {
            model.Order orderObject = orderContainer.getOrder().getOrderObject();
            
            if (orderObject.getOrder_date().length() > 0)
            {
                JLabel lblOrderDate = new JLabel("<html><b>Ordredato:</b> "+orderObject.getOrder_date()+"</html>");
                lblOrderDate.setFont(DesignLayout.STANDARD.deriveFont(11f));
                lblOrderDate.setBounds(300, 20, 250, 12);
                lblOrderDate.setHorizontalAlignment(SwingConstants.RIGHT);
                overviewPanel.add(lblOrderDate);
            }
            
            if (orderContainer.getOrder().getOrderObject().getCustomerSpecified()[0] != null)
            {
                JPanel customerPanel = getCustomerPanel("Kundeoplysninger", orderObject.getCustomerSpecified()[0]);
                customerPanel.setBounds(25, 40, 250, 60);
                overviewPanel.add(customerPanel);
            }

            if (orderContainer.getOrder().getOrderObject().getCustomerSpecified()[1] != null)
            {
                JPanel invoicePanel = getCustomerPanel("Faktureringsadresse", orderObject.getCustomerSpecified()[1]);
                invoicePanel.setBounds(300, 40, 250, 60);
                overviewPanel.add(invoicePanel);
            }
            
            if (!orderObject.getPickup_date().equals("00.00.0000"))
            {
                JLabel lblPickupDate = new JLabel("<html><b>Afhentningsdato:</b> "+orderObject.getPickup_date()+"</html>");
                lblPickupDate.setFont(DesignLayout.STANDARD.deriveFont(11f));
                lblPickupDate.setBounds(25, 112, 250, 12);
                overviewPanel.add(lblPickupDate);
            }
            
            if (!orderObject.getDelivery_date().equals("00.00.0000"))
            {
                JLabel lblDeliveryDate = new JLabel("<html><b>Leveringsdato:</b> "+orderObject.getDelivery_date()+"</html>");
                lblDeliveryDate.setFont(DesignLayout.STANDARD.deriveFont(11f));
                lblDeliveryDate.setBounds(25, 125, 250, 12);
                overviewPanel.add(lblDeliveryDate);
            }            
            
            if (orderObject.getTombStone() != null)
            {
                JPanel graveYardPanel = getGraveYardInfo(orderObject.getTombStone());
                graveYardPanel.setBounds(25, 145, 250, 90);
                overviewPanel.add(graveYardPanel);
                
                if (orderObject.getTombStone().getMortician() != null)
                {
                    JLabel lblMorticianHeadline = new JLabel("Bedemand (provision: "+orderObject.getTombStone().getCommision()+"%)");
                    lblMorticianHeadline.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD, 11f));
                    lblMorticianHeadline.setBounds(300, 112, 250, 12);
                    overviewPanel.add(lblMorticianHeadline);
                    
                    JLabel lblMortician = new JLabel(orderObject.getTombStone().getMortician().getCustomerName().getName());
                    lblMortician.setFont(DesignLayout.STANDARD.deriveFont(11f));
                    lblMortician.setBounds(310, 125, 250, 12);
                    overviewPanel.add(lblMortician);
                }
                
                if (orderObject.getTombStone().getOldName().size() > 0)
                {
                    JLabel lblExistingName = new JLabel("Eksisterende Navn");
                    lblExistingName.setBounds(300, 145, 250, 12);
                    lblExistingName.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD, 11f));

                    JLabel lblExistingNames = new JLabel("");
                    lblExistingNames.setText(orderObject.getTombStone().getOldName().get(0));

                    lblExistingNames.setBounds(310, 158, 250, 12);
                    lblExistingNames.setFont(DesignLayout.STANDARD.deriveFont(11f));
                    overviewPanel.add(lblExistingName);
                    overviewPanel.add(lblExistingNames);
                }
                
                JLabel lblInscription = new JLabel("Inskription ("+orderObject.getTombStone().getNameCharacterCount()+" tegn)", JLabel.CENTER);
                lblInscription.setBounds(300, 180, 250, 12);
                lblInscription.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD, 11f));
                overviewPanel.add(lblInscription);

                JTextPane tPaneInscrption = new JTextPane();
                tPaneInscrption.setEditable(false);
                tPaneInscrption.setFont(DesignLayout.STANDARD.deriveFont(Font.PLAIN, 11f));
                StyledDocument docInscription = tPaneInscrption.getStyledDocument();
                SimpleAttributeSet centerInscription = new SimpleAttributeSet();
                StyleConstants.setAlignment(centerInscription, StyleConstants.ALIGN_CENTER);
                docInscription.setParagraphAttributes(0, docInscription.getLength(), centerInscription, false);

                for (String value : orderObject.getTombStone().getInscription())
                {
                    try
                    {
                        docInscription.insertString(docInscription.getLength(), value, null);
                    } 
                    catch (BadLocationException ex)
                    {
                        ExceptionManager.handleBadLocationException(ex);
                    }
                }

                JScrollPane inscriptionScroll = new JScrollPane();
                inscriptionScroll.setBorder(DesignLayout.T_FIELD_RED);
                inscriptionScroll.setBackground(DesignLayout.T_FIELD_BG_COLOR);
                inscriptionScroll.setBounds(300, 195, 257, 60);
                inscriptionScroll.setViewportView(tPaneInscrption);
                inscriptionScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                inscriptionScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                inscriptionScroll.getVerticalScrollBar().setUnitIncrement(15);
                overviewPanel.add(inscriptionScroll);
            }
            
            buildProductPanel();
            buildProductScrollPanel();
            buildSumPanel();
             
            JPanel commentPanel = new JPanel(null);
            commentPanel.setBounds(20, 415, 540, 65);
            commentPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Bemærkninger", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD.deriveFont(12f)));
            commentPanel.setBackground(Color.WHITE);
            overviewPanel.add(commentPanel);
            
            JTextPane comments = new JTextPane();
            comments.setEditable(false);
            comments.setBounds(20, 20, 500, 40);
            comments.setFont(DesignLayout.STANDARD.deriveFont(11f));
            commentPanel.add(comments);
            
            StyledDocument commentDoc = comments.getStyledDocument();
            try
            {
                commentDoc.insertString(0, orderObject.getComment(), null);
            } 
            catch (BadLocationException ex)
            {
                ExceptionManager.handleBadLocationException(ex);
            }
        }
        
        overviewPanel.revalidate();
        overviewPanel.repaint();
    }
    
    private JPanel getCustomerPanel(String headline, CustomerSpecified customer)
    {
        JPanel panel = new JPanel(null);
        panel.setOpaque(false);
        panel.setBounds(0, 0, 250, 60);     
        
        JLabel lblHeadline = new JLabel(headline);
        lblHeadline.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD, 11f));
        lblHeadline.setBounds(0, 0, 230, 12);
        
        JLabel lblName = new JLabel(customer.getCustomerName().getName());
        lblName.setFont(DesignLayout.STANDARD.deriveFont(11f));
        lblName.setBounds(10, 12, 230, 12);
        
        JLabel lblAddress = new JLabel(customer.getCustomerAddress().getStreet());
        lblAddress.setFont(DesignLayout.STANDARD.deriveFont(11f).deriveFont(11f));
        lblAddress.setBounds(10, 24, 230, 12);
        
        JLabel lblZipCity = new JLabel(customer.getCustomerAddress().getZipCode().toString());
        lblZipCity.setFont(DesignLayout.STANDARD.deriveFont(11f));
        lblZipCity.setBounds(10, 36, 230, 12);
        
        JLabel lblTelephone = new JLabel(customer.getCustomerTelephone().getNumber());
        lblTelephone.setFont(DesignLayout.STANDARD.deriveFont(11f));
        lblTelephone.setBounds(10, 48, 230, 12);
        
        panel.add(lblHeadline);
        panel.add(lblName);
        panel.add(lblAddress);
        panel.add(lblZipCity);
        panel.add(lblTelephone);
        return panel;
    }
    
    private JPanel getGraveYardInfo(model.TombStone tombstone)
    {
        JPanel panel = new JPanel(null);
        panel.setOpaque(false);
        panel.setBounds(0, 0, 250, 90);
        
        String cuffin = "";
        if (tombstone.isCuffin() == true)
        {
            cuffin = "Kiste";
        }
        else
        {
            cuffin = "Urne";
        }
        JLabel lblCuffin = new JLabel("<html><b>Urne/Kiste:</b> "+cuffin+"</html>");
        lblCuffin.setFont(DesignLayout.STANDARD.deriveFont(11f));
        lblCuffin.setBounds(0, 0, 230, 12);
        
        JLabel lblGraveYard = new JLabel("<html><b>Kirkegård:</b> "+tombstone.getGraveYard().getName()+"</html>");
        lblGraveYard.setFont(DesignLayout.STANDARD.deriveFont(11f));
        lblGraveYard.setBounds(0, 13, 230, 12);
        
        JLabel lblDepartment = new JLabel("<html><b>Afdeling:</b> "+tombstone.getArea()+"</html>");
        lblDepartment.setFont(DesignLayout.STANDARD.deriveFont(11f));
        lblDepartment.setBounds(0, 26, 230, 12);
        
        JLabel lblRowNumber = new JLabel("<html><b>Række:</b> "+tombstone.getTheRow()+" <b>Nummer:</b> "+tombstone.getTheNumber()+"</html>");
        lblRowNumber.setFont(DesignLayout.STANDARD.deriveFont(11f));
        lblRowNumber.setBounds(0, 39, 230, 12);
        
        JLabel lblRepaint = new JLabel("<html><b>Opmaling/Oppudsning:</b> "+utility.Converter.booleanToString(tombstone.isRepaint())+"</html>");
        lblRepaint.setFont(DesignLayout.STANDARD.deriveFont(11f));
        lblRepaint.setBounds(0, 52, 230, 12);
        
        JLabel lblFont = new JLabel("<html><b>Skrifttype:</b> "+tombstone.getFont()+"</html>");
        lblFont.setFont(DesignLayout.STANDARD.deriveFont(11f));
        lblFont.setBounds(0, 65, 230, 12);
        
        JLabel lblNameSpace = new JLabel("<html><b>Plads til "+tombstone.getNumNamesRoomFor()+" navn(e)</b></html>");
        lblNameSpace.setFont(DesignLayout.STANDARD.deriveFont(11f));
        lblNameSpace.setBounds(0, 78, 230, 12);
        
        panel.add(lblCuffin);
        panel.add(lblGraveYard);
        panel.add(lblDepartment);
        panel.add(lblRowNumber);
        panel.add(lblRepaint);
        panel.add(lblFont);
        panel.add(lblNameSpace);
        return panel;
    }
    
    private void buildProductPanel()
    {
        JPanel productTitles = new JPanel(null);
        productTitles.setBounds(20, 255, 500, 25);
        productTitles.setOpaque(false);
        
        JLabel lblName = new JLabel("Navn");
        lblName.setBounds(35, 0, 275, 25);
        lblName.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD, 11f));
        
        JLabel lblAmount = new JLabel("Antal", JLabel.CENTER);
        lblAmount.setBounds(310, 0, 50, 25);
        lblAmount.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD, 11f));
        
        JLabel lblUnitPrice = new JLabel("Stk Pris", JLabel.CENTER);
        lblUnitPrice.setBounds(360, 0, 50, 25);
        lblUnitPrice.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD, 11f));
        
        JLabel lblPrice = new JLabel("Pris", JLabel.CENTER);
        lblPrice.setBounds(410, 0, 100, 25);
        lblPrice.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD, 11f));
        
        productTitles.add(lblName);
        productTitles.add(lblAmount);
        productTitles.add(lblUnitPrice);
        productTitles.add(lblPrice);
        
        productPanel = new JPanel(null);
        productPanel.setBounds(20, 275, 540, 125);
        productPanel.setBackground(Color.WHITE);
        
        scrollProductPanel = new JPanel(null);
        scrollProductPanel.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        scrollProductPanel.setBackground(Color.white);
        scrollProductPanel.setBorder(null);
        
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBorder(DesignLayout.T_FIELD_RED);
        scrollPane.setBounds(0, 0, 540, 125);
        scrollPane.setViewportView(scrollProductPanel);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.getVerticalScrollBar().setUnitIncrement(15);
        
        productPanel.add(scrollPane);
        productPanel.setVisible(true);
        overviewPanel.add(productTitles);
        overviewPanel.add(productPanel);
    }
    
    private void buildProductScrollPanel()
    {
        scrollProductPanel.removeAll();
        
        ArrayList<OrderLine> orderLines = orderContainer.getOrder().getOrderObject().getOrderLine();
        for (int i = 0; i < orderLines.size(); i++)
        {
            OrderOverviewProductListPanel panel = new OrderOverviewProductListPanel(this, orderLines.get(i));
            panel.setBounds(10, 10 + (i * 25), 500, 25);
            scrollProductPanel.setPreferredSize(new Dimension(500, 10 + (i * 25)));
            scrollProductPanel.add(panel);
        }
        
        if (orderLines.size() == 0)
        {
            JPanel panel = new JPanel(null);
            panel.setOpaque(false);
            panel.setBounds(10, 10, 500, 25);
            scrollProductPanel.add(panel);
            
            JLabel label = new JLabel("<html><div style='text-align:center; width: 400px;'><b>Ingen varer tilføjet</b></div></html>");
            label.setBounds(0, 0, 500, 25);
            label.setOpaque(true);
            label.setBorder(DesignLayout.T_FIELD_RED);
            label.setBackground(DesignLayout.T_FIELD_BG_COLOR);
            
            panel.add(label);
        }
        
        scrollProductPanel.revalidate();
        scrollProductPanel.repaint();       
    }
    
    private void buildSumPanel()
    {
        model.Order orderObject = orderContainer.getOrder().getOrderObject();
        
        JPanel sumPanel = new JPanel(null);
        sumPanel.setBounds(20, 397, 540, 25);
        sumPanel.setOpaque(false);
        
        DecimalFormat df = new DecimalFormat("#.##");
        
        JLabel lblBeforeTax = new JLabel("<html>"+df.format(orderObject.totalPrice())+" kroner <b>før miljø</b></html>");
        lblBeforeTax.setFont(DesignLayout.STANDARD.deriveFont(11f));
        lblBeforeTax.setBounds(0, 0, 180, 25);
        
        JLabel lblAfterEnvironment = new JLabel("<html>"+df.format(orderObject.totalPriceWithEnvironment())+" kroner <b>inkl miljø</b></html>");
        lblAfterEnvironment.setFont(DesignLayout.STANDARD.deriveFont(11f));
        lblAfterEnvironment.setBounds(180, 0, 180, 25);
        
        JLabel lblAfterVat = new JLabel("<html>"+df.format(orderObject.totalPriceVat())+" kroner <b>inkl moms</b></html>", JLabel.RIGHT);
        lblAfterVat.setFont(DesignLayout.STANDARD.deriveFont(11f));
        lblAfterVat.setBounds(360, 0, 180, 25);
        
        sumPanel.add(lblBeforeTax);
        sumPanel.add(lblAfterEnvironment);
        sumPanel.add(lblAfterVat);
        
        overviewPanel.add(sumPanel);
    }

    public OrderContainer getOrderContainer()
    {
        return orderContainer;
    }

    public JPanel getPanel() {
        return overviewPanel;
    }
}